<template>
  <up-popup 
    :show="show" 
    @close="handleClose" 
    mode="center" 
    :round="20"
    :closeable="true"
    :customStyle="{
      width: '650rpx',
      maxHeight: '80vh',
      overflow: 'hidden'
    }"
  >
    <view class="unconfirmed-order-modal">
      <view class="modal-header">
        <text class="modal-title">处理未确认订单</text>
      </view>
      
      <view class="modal-content">
        <view class="order-info" v-if="orderData">
          <view class="info-item">
            <text class="info-label">任务名称：</text>
            <text class="info-value">{{ orderData.taskName }}</text>
          </view>
          <view class="info-item">
            <text class="info-label">未确认订单数：</text>
            <text class="info-value highlight">{{ orderData.unconfirmedCount }}</text>
          </view>
        </view>
        
        <view class="action-section">
          <text class="section-title">处理方式</text>
          <up-radio-group v-model="formData.actionType" @change="handleActionChange">
            <up-radio 
              name="batch_confirm" 
              label="批量确认所有订单"
              :customStyle="{
                marginBottom: '20rpx'
              }"
            />
            <up-radio 
              name="batch_reject" 
              label="批量驳回所有订单"
              :customStyle="{
                marginBottom: '20rpx'
              }"
            />
            <up-radio 
              name="manual_review" 
              label="逐个手动审核"
            />
          </up-radio-group>
        </view>
        
        <view class="reason-section" v-if="formData.actionType === 'batch_reject'">
          <text class="section-title">驳回原因</text>
          <up-textarea 
            v-model="formData.rejectReason"
            placeholder="请输入驳回原因"
            :maxlength="200"
            :customStyle="{
              backgroundColor: '#f8f9fa',
              borderRadius: '12rpx',
              padding: '20rpx',
              minHeight: '120rpx'
            }"
          />
        </view>
        
        <view class="confirm-section" v-if="formData.actionType === 'batch_confirm'">
          <text class="section-title">确认信息</text>
          <view class="confirm-item">
            <up-checkbox 
              v-model="formData.confirmNotification"
              label="发送确认通知给相关人员"
              :customStyle="{
                marginBottom: '20rpx'
              }"
            />
          </view>
          <view class="confirm-item">
            <up-checkbox 
              v-model="formData.updateStatus"
              label="自动更新任务状态"
            />
          </view>
        </view>
        
        <view class="manual-section" v-if="formData.actionType === 'manual_review'">
          <text class="section-title">手动审核说明</text>
          <view class="manual-info">
            <text class="manual-text">选择此选项将跳转到详细的订单审核页面，您可以逐个查看和处理每个未确认的订单。</text>
          </view>
        </view>
      </view>
      
      <view class="modal-footer">
        <up-button 
          text="取消" 
          @click="handleClose"
          :customStyle="{
            flex: 1,
            marginRight: '20rpx',
            height: '80rpx',
            backgroundColor: '#f5f5f5',
            color: '#666666',
            border: 'none'
          }"
        />
        <up-button 
          :text="getConfirmButtonText()"
          type="primary"
          @click="handleConfirm"
          :loading="loading"
          :customStyle="{
            flex: 1,
            height: '80rpx'
          }"
        />
      </view>
    </view>
  </up-popup>
</template>

<script setup lang="ts">
import { ref, reactive, watch } from 'vue'

// Props
interface Props {
  show: boolean
  orderData?: any
}

const props = withDefaults(defineProps<Props>(), {
  show: false,
  orderData: null
})

// Emits
const emit = defineEmits<{
  'update:show': [value: boolean]
  'confirm': [data: any]
}>()

// 响应式数据
const loading = ref(false)

// 表单数据
const formData = reactive({
  actionType: 'batch_confirm',
  rejectReason: '',
  confirmNotification: true,
  updateStatus: true
})

// 监听显示状态
watch(() => props.show, (newVal) => {
  if (newVal) {
    resetForm()
  }
})

// 重置表单
const resetForm = () => {
  Object.assign(formData, {
    actionType: 'batch_confirm',
    rejectReason: '',
    confirmNotification: true,
    updateStatus: true
  })
}

// 关闭弹窗
const handleClose = () => {
  emit('update:show', false)
}

// 处理方式变化
const handleActionChange = (value: string) => {
  formData.actionType = value
  if (value !== 'batch_reject') {
    formData.rejectReason = ''
  }
}

// 获取确认按钮文本
const getConfirmButtonText = () => {
  switch (formData.actionType) {
    case 'batch_confirm':
      return '批量确认'
    case 'batch_reject':
      return '批量驳回'
    case 'manual_review':
      return '开始审核'
    default:
      return '确认'
  }
}

// 确认处理
const handleConfirm = async () => {
  // 表单验证
  if (formData.actionType === 'batch_reject' && !formData.rejectReason.trim()) {
    uni.showToast({
      title: '请输入驳回原因',
      icon: 'none'
    })
    return
  }
  
  loading.value = true
  
  try {
    const actionData = {
      taskId: props.orderData?.id,
      actionType: formData.actionType,
      rejectReason: formData.rejectReason,
      confirmNotification: formData.confirmNotification,
      updateStatus: formData.updateStatus
    }
    
    if (formData.actionType === 'manual_review') {
      // 跳转到手动审核页面
      uni.navigateTo({
        url: `/pages/vehicle-internal-verify/manual-review?taskId=${props.orderData?.id}`
      })
      emit('update:show', false)
    } else {
      // 批量处理
      emit('confirm', actionData)
    }
  } catch (error) {
    console.error('处理失败:', error)
  } finally {
    loading.value = false
  }
}
</script>

<style lang="scss" scoped>
.unconfirmed-order-modal {
  background-color: #ffffff;
  border-radius: 20rpx;
  overflow: hidden;
}

.modal-header {
  padding: 40rpx 30rpx 30rpx;
  text-align: center;
  border-bottom: 1rpx solid #f0f0f0;
}

.modal-title {
  font-size: 36rpx;
  font-weight: 600;
  color: #333333;
}

.modal-content {
  padding: 30rpx;
  max-height: 60vh;
  overflow-y: auto;
}

.order-info {
  background-color: #f8f9fa;
  border-radius: 12rpx;
  padding: 24rpx;
  margin-bottom: 30rpx;
}

.info-item {
  display: flex;
  margin-bottom: 16rpx;
  
  &:last-child {
    margin-bottom: 0;
  }
}

.info-label {
  font-size: 28rpx;
  color: #666666;
  min-width: 200rpx;
}

.info-value {
  font-size: 28rpx;
  color: #333333;
  flex: 1;
  
  &.highlight {
    color: #ff4d4f;
    font-weight: 600;
  }
}

.action-section,
.reason-section,
.confirm-section,
.manual-section {
  margin-bottom: 30rpx;
  
  &:last-child {
    margin-bottom: 0;
  }
}

.section-title {
  display: block;
  font-size: 30rpx;
  color: #333333;
  margin-bottom: 20rpx;
  font-weight: 600;
}

.confirm-item {
  margin-bottom: 16rpx;
  
  &:last-child {
    margin-bottom: 0;
  }
}

.manual-info {
  background-color: #e6f7ff;
  border-radius: 12rpx;
  padding: 24rpx;
  border-left: 4rpx solid #1890ff;
}

.manual-text {
  font-size: 26rpx;
  color: #1890ff;
  line-height: 1.6;
}

.modal-footer {
  padding: 30rpx;
  border-top: 1rpx solid #f0f0f0;
  display: flex;
  gap: 20rpx;
}
</style>